package com.properdsa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.cmmunity.beans.Housing;
import cn.cmmunity.beans.User;
import com.properdsa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/userlist")
    public String userlist(@RequestParam(value = "name",required = false)Integer name,
                @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum
                , HttpServletRequest request) {

        Map<String,Object>map=new HashMap<>();
         map.put("name",name);
         Integer count=userService.count(map);
        PageHelper.startPage(pageNum, 5);
        List<User>list=userService.userList(map);
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        pageInfo.setTotal(count);
        request.setAttribute("pageInfo",pageInfo);
        return "dd";
    }

    @RequestMapping("/getAllName")
    public String getAllName(HttpServletRequest request){
        List<Housing>getName=userService.getAllName();
        request.setAttribute("getName",getName);
        return "add";
    }

        @RequestMapping("/add")
        public String add(String username,String phone,String address,
                      String remark,String password,String name,
                      Integer housingid,String gender){
             User user=new User();
             user.setUsername(username);
             user.setPhone(phone);
             user.setAddress(address);
             user.setRemark(remark);
             user.setPassword(password);
             user.setName(name);
             user.setHousingId(housingid);
             user.setGender(gender);
             int result=userService.add(user);
             if (result>0){
                 return "<script>alert('新增成功');location.href='dd';</script>";
             }else {
                 return "<script>alert('新增失败');location.href='dd';</script>";
             }
    }
    @RequestMapping("/getAlllist")
    public String getAlllist(HttpServletRequest request){
            List<User>users=userService.getAlllist();
        request.setAttribute("users",users);
        return "update";
    }
         @RequestMapping("/update")
         public String update(Integer id){
            Integer result=userService.update(id);
            if (result>0){
                return "<script>alert('修改成功');location.href='dd';</script>";
            }else {
                return "<script>alert('修改失败');location.href='dd';</script>";
            }
    }
    @RequestMapping("/delete")
    public String delete(Integer id){
             Integer result=userService.delete(id);
             if (result>0){
                 return "<script>alert('删除成功');location.href='dd';</script>";
             }else{
                 return "<script>alert('删除失败');location.href='dd';</script>";
             }
    }
}
